package com.application.sistercityapplication.listadapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.application.sistercityapplication.dto.ContactDto;
import com.application.sistercityapplication.main.R;
import com.squareup.picasso.Picasso;

public class ContactListAdapter extends BaseAdapter{
	private List<ContactDto> _docList;
	private ViewHolder viewHolder;
	private Context _context;
	private Activity _activity;
	private ContactDto magazineObj;
	public ContactListAdapter(List<ContactDto> docList,Context con,Activity activity) {
		// TODO Auto-generated constructor stub
		this._docList = docList;
		this._context = con;
		this._activity = activity;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return _docList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return _docList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		if(convertView== null){
			viewHolder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.contact_list_each_item, null);
			viewHolder.documentThumbnail = (ImageView) convertView.findViewById(R.id.documentThumbnail);
			viewHolder.detail = (TextView) convertView.findViewById(R.id.countryName);
			viewHolder.documentTitle = (TextView) convertView.findViewById(R.id.text_item);
			viewHolder.phoneBt = (ImageView) convertView.findViewById(R.id.imageView1);
			
			viewHolder.phoneBt.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+magazineObj.getPhone().replace("-", ""))); 
					_activity.startActivity(callIntent);
				}
			});
			
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		magazineObj = _docList.get(position);
		Picasso.with(_context).load(magazineObj.getImagePath()).into(viewHolder.documentThumbnail);
		viewHolder.documentTitle.setText(magazineObj.getTitle());
		viewHolder.detail.setText(magazineObj.getDetail());
		try{
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return convertView;
	}
	public class ViewHolder{
		private ImageView documentThumbnail,phoneBt;
		private TextView documentTitle;
		private TextView detail;
	}
}
